package org.zjx.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.zjx.common.bean.Result;
import org.zjx.common.util.BeanUtil;
import org.zjx.dao.dto.PropDTO;
import org.zjx.dao.entity.Font;
import org.zjx.dao.entity.FontType;
import org.zjx.dao.vo.FontVO;
import org.zjx.service.IFontService;
import org.zjx.service.IFontTypeService;
import org.zjx.service.IPropService;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2025-05-30
 */
@Tag(name = "字体管理接口")
@RestController
@RequestMapping("/font")
@RequiredArgsConstructor
public class FontController {

    final private IFontService fontService;
    final private IFontTypeService fontTypeService;

    @Operation(summary = "上传字库")
    @PostMapping("/saveFont")
    public Result saveFont(@RequestBody Font font) {
        return  fontService.saveFont(font);
    }

    @Operation(summary = "根据类型获取字库")
    @GetMapping("/selectFont/{fontType}")
    public Result<List<FontVO>> selectFont(@Parameter(description = "字库类型") @PathVariable("fontType") String fontType) {
        try {
            Long fontTypeId = Long.parseLong(fontType);
            List<Font> fontList = fontService.selectByFontTypeId(fontTypeId);

            List<FontVO> fontVOS = new ArrayList<>();
            for (Font font : fontList) {
                FontVO fontVO = BeanUtil.copyProperties(font, FontVO.class);
                fontVO.setFontType(fontType);
                fontVOS.add(fontVO);

            }

            return Result.isOk().data(fontVOS);
        } catch (NumberFormatException e) {
            return Result.isFail().msg(new Exception("参数格式错误：fontType必须为数字"));
        }
    }


    @Operation(summary = "获取所有字库")
    @GetMapping("/selectAllFonts")
    public Result<List<FontVO>> selectAllFonts() {
        try {
            List<FontType> fontTypes = fontTypeService.list();

            List<Font> fontList = fontService.list();
            List<FontVO> fontVOS = new ArrayList<>();
            for (Font font : fontList) {
                FontVO fontVO = BeanUtil.copyProperties(font, FontVO.class);
                for (FontType fontType : fontTypes) {
                    if (Objects.equals(font.getFontTypeId(), fontType.getId())) {
                        fontVO.setFontType(fontType.getName());
                        fontVO.setType(font.getFontTypeId());
                        fontVOS.add(fontVO);
                        break;
                    }
                }
            }


            return Result.isOk().data(fontVOS);
        } catch (NumberFormatException e) {
            return Result.isFail().msg(new Exception("参数格式错误：fontType必须为数字"));
        }
    }

}
